/**
 *
 */
package vn.tecviets.thuandt.model.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import vn.tecviets.thuandt.model.bean.Category;
import vn.tecviets.thuandt.model.bean.Post;
import vn.tecviets.thuandt.model.common.ConnectionProvider;

/*
 * CategoryDAO
 * 1.0
 * Mar 24, 2013
 * 
 * Copyright notice
 * 
 * Modification Logs:
 * 
 * DATE					AUTHOR				DESCRIPTION
 * -----------------------------------------------------------
 * Mar 24, 2013			Thuandt				Create
 */

public class CategoryDAO {

	private Connection con = null;
	private CallableStatement cs = null;
	private ResultSet rs = null;
	private String sql = null;

	
	/**
	 * @return
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 */
	public ArrayList<Category> getAllCategory() throws ClassNotFoundException, SQLException {
		sql = "{call sp_SelectAllCategory}";
		ArrayList<Category> categoryArrayList = new ArrayList<Category>();

		try {
			con = ConnectionProvider.getConnection();
			cs = con.prepareCall(sql);
			rs = cs.executeQuery();

			while (rs.next()) {
				Category category = new Category();
				category.setCategoryId(rs.getInt("CategoryId"));
				category.setCategoryTitle(rs.getNString("CategoryTitle"));
				categoryArrayList.add(category);
			}
		} catch (ClassNotFoundException e) {
			throw e;
		} catch (SQLException e) {
			throw e;
		} finally {
			if (rs != null) {
				rs.close();
			}

			if (cs != null) {
				cs.close();
			}

			if (con != null) {
				con.close();
			}
		}

		return categoryArrayList;
	}

}
